package com.czl.module_service.adapter

import android.annotation.SuppressLint
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.viewholder.BaseDataBindingHolder
import com.czl.base.binding.command.BindingCommand
import com.czl.base.binding.command.BindingConsumer
import com.czl.base.config.AppConstants
import com.czl.base.data.bean.tengyun.SortBean
import com.czl.module_service.R
import com.czl.module_service.databinding.ItemSortManagementBinding
import com.czl.module_service.event.SortBeanEvent
import com.czl.module_service.fragment.sort.SortManagementFragment
import com.jeremyliao.liveeventbus.LiveEventBus

/**
 * @author by xubin,
 * @date on 2022/1/19.
 * PS: Not easy to write code, please indicate.
 */
class SortManagementAdapter(private val fragment: SortManagementFragment) :
    BaseQuickAdapter<SortBean, BaseDataBindingHolder<ItemSortManagementBinding>>(
        R.layout.item_sort_management
    ) {

    var isEdit: Boolean = true
        @SuppressLint("NotifyDataSetChanged")
        set(value) {
            field = value
            notifyDataSetChanged()
        }

    var listener: OnDeleteListener? = null

    override fun convert(
        holder: BaseDataBindingHolder<ItemSortManagementBinding>,
        item: SortBean,
    ) {
        holder.dataBinding?.run {
            data = item
            edit = isEdit
            delete = !isEdit
            adapter = this@SortManagementAdapter
            executePendingBindings()
        }
    }

    val onDeleteSortClick: BindingCommand<Any> = BindingCommand(BindingConsumer {
        if (it is SortBean) {
            listener?.onDelete(it)
        }
    })

    val onItemClick: BindingCommand<Any> = BindingCommand(BindingConsumer {
        if (it is SortBean) {
            LiveEventBus.get(SortBeanEvent::class.java).post(SortBeanEvent(it))
            fragment.startContainerActivity(
                AppConstants.Router.Service.F_SERVICE_SORT_ADD
            )
        }
    })
}

interface OnDeleteListener {
    fun onDelete(bean: SortBean)
}